{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "## 1.参数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bd83a2ab2f812a39"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n"
     ]
    }
   ],
   "source": [
    "def get_info():\n",
    "    print(123)\n",
    "\n",
    "\n",
    "get_info()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:18:32.777092400Z",
     "start_time": "2024-01-15T02:18:32.677888800Z"
    }
   },
   "id": "e77c905651a2fc69",
   "execution_count": 2
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123\n",
      "456\n"
     ]
    }
   ],
   "source": [
    "def send_email(who):\n",
    "    print(who)\n",
    "\n",
    "\n",
    "send_email(123)\n",
    "send_email(456)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:18:36.875817500Z",
     "start_time": "2024-01-15T02:18:36.761557300Z"
    }
   },
   "id": "41f2daeb478fb7bd",
   "execution_count": 3
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 参数的传递\n",
    "参数传递的是引用，不会重新拷贝一份数据"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c2398af239ba4603"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "wsl\n"
     ]
    }
   ],
   "source": [
    "# 在内存中有'wsl',name指向'wsl',name作为函数的参数传递给arg,传递的引用，而不是复制一份数据。\n",
    "def func(arg):\n",
    "    print(arg)\n",
    "\n",
    "\n",
    "name = \"wsl\"\n",
    "func(name)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:18:41.104267700Z",
     "start_time": "2024-01-15T02:18:40.973538200Z"
    }
   },
   "id": "36a3aa71d816650a",
   "execution_count": 4
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11, 22, 33, 999]\n"
     ]
    }
   ],
   "source": [
    "def func(a1):\n",
    "    a1.append(999)\n",
    "\n",
    "\n",
    "v1 = [11, 22, 33]\n",
    "func(v1)\n",
    "\n",
    "print(v1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:18:44.838852100Z",
     "start_time": "2024-01-15T02:18:44.676553100Z"
    }
   },
   "id": "18bace81d31edc6c",
   "execution_count": 5
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11, 33]\n"
     ]
    }
   ],
   "source": [
    "def func(a1):\n",
    "    a1.remove(22)\n",
    "\n",
    "\n",
    "v1 = [11, 22, 33]\n",
    "func(v1)\n",
    "\n",
    "print(v1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:18:49.932421Z",
     "start_time": "2024-01-15T02:18:49.761392100Z"
    }
   },
   "id": "850cbc4e63559364",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "root\n"
     ]
    }
   ],
   "source": [
    "# 传递的还是引用 只不过str是不可变的，调用方法后会再生成一个str.\n",
    "def func(a1):\n",
    "    x = a1.upper()\n",
    "\n",
    "\n",
    "v1 = \"root\"\n",
    "func(v1)\n",
    "print(v1)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:18:58.496256500Z",
     "start_time": "2024-01-15T02:18:58.348279300Z"
    }
   },
   "id": "5061988e238d0324",
   "execution_count": 7
  },
  {
   "cell_type": "markdown",
   "source": [
    "![jupyter](./png/img.png)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d2d1c1f51428b293"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 默认参数\n",
    "默认参数中出现了 `可变类型` 一定要谨慎（list/dict）"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8c6ed7bcced5b7c0"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 2 999\n",
      "1 2 3\n"
     ]
    }
   ],
   "source": [
    "# 默认值\n",
    "def send_email(x1, x2, x3=999):\n",
    "    print(x1, x2, x3)\n",
    "\n",
    "\n",
    "send_email(1, 2)\n",
    "send_email(1, 2, 3)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:19:17.015452500Z",
     "start_time": "2024-01-15T02:19:16.878036200Z"
    }
   },
   "id": "933e5da7b3919acb",
   "execution_count": 8
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2]\n",
      "[1, 2]\n",
      "[2, 2]\n"
     ]
    }
   ],
   "source": [
    "def func(a2=[]):\n",
    "    a2.append(2)\n",
    "    print(a2)\n",
    "\n",
    "\n",
    "func()\n",
    "func([1, ])\n",
    "func()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:21:47.256682300Z",
     "start_time": "2024-01-15T02:21:47.120580600Z"
    }
   },
   "id": "a29ad49ee0c669ac",
   "execution_count": 9
  },
  {
   "cell_type": "markdown",
   "source": [
    "###  传参的指定"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c107c4dee747ecbf"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123 345\n",
      "123 345\n",
      "123 345\n",
      "999 666\n"
     ]
    }
   ],
   "source": [
    "def send_email(who1, who2):\n",
    "    print(who1, who2)\n",
    "\n",
    "\n",
    "# 位置传参\n",
    "send_email(123, 345)\n",
    "\n",
    "# 关键字传参\n",
    "send_email(who1=123, who2=345)\n",
    "send_email(who2=345, who1=123)\n",
    "\n",
    "# 混合（关键字传参放在后面）\n",
    "send_email(999, who2=666)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:22:02.630166300Z",
     "start_time": "2024-01-15T02:22:02.511530900Z"
    }
   },
   "id": "1da9af529099bcb7",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a出现3次\n"
     ]
    }
   ],
   "source": [
    "def get_str_count(data_str):\n",
    "    count = 0\n",
    "    for item in data_str:\n",
    "        if item == 'a':\n",
    "            count = count + 1\n",
    "    print('a出现{}次'.format(count))\n",
    "\n",
    "\n",
    "get_str_count('ababa')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:22:08.457217700Z",
     "start_time": "2024-01-15T02:22:08.326075Z"
    }
   },
   "id": "ba7474f22051ebe8",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a出现3次\n",
      "a出现3次\n"
     ]
    }
   ],
   "source": [
    "def get_str_count(data_str, key='a'):\n",
    "    count = 0\n",
    "    for item in data_str:\n",
    "        if item == key:\n",
    "            count = count + 1\n",
    "    print('{}出现{}次'.format(key, count))\n",
    "\n",
    "\n",
    "get_str_count('ababa')\n",
    "get_str_count('ababa', 'a')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:22:18.665362300Z",
     "start_time": "2024-01-15T02:22:18.521724Z"
    }
   },
   "id": "f4b53f5251de4cf2",
   "execution_count": 12
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 动态参数\n",
    "*   参数个数任意 参数是元祖\n",
    "**  键值对传参  参数是字典\n",
    "*** 组合"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "299e929751976290"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 'xxx', [11, 22])\n"
     ]
    }
   ],
   "source": [
    "def func(*a1):\n",
    "    # a1=(1,'xxx',[11,22])\n",
    "    print(a1)\n",
    "\n",
    "\n",
    "func(1, 'xxx', [11, 22])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:22:26.292235900Z",
     "start_time": "2024-01-15T02:22:26.192260600Z"
    }
   },
   "id": "d3a54cc71e609945",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'v1': 123, 'v2': 456}\n"
     ]
    }
   ],
   "source": [
    "def func2(**a2):\n",
    "    # a2={'v1'=123,'v2':456}\n",
    "    print(a2)\n",
    "\n",
    "\n",
    "func2(v1=123, v2=456)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:22:32.954815800Z",
     "start_time": "2024-01-15T02:22:32.787599700Z"
    }
   },
   "id": "2bdf47dc853a706",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func(*args, **kwargs):\n",
    "    pass"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:22:40.957752500Z",
     "start_time": "2024-01-15T02:22:40.830607100Z"
    }
   },
   "id": "4523b40a4f5d8a00",
   "execution_count": 15
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 2) {}\n",
      "() {'v1': 123, 'v2': 456}\n",
      "(11,) {'data': 123}\n"
     ]
    }
   ],
   "source": [
    "def func(*a1, **a2):\n",
    "    # a1=(1,2)  a2={}\n",
    "    # a1=()     a2={\"v1\":123,\"v2\":456}\n",
    "    # a1=(11,)  a2={\"data\":123}\n",
    "    print(a1, a2)\n",
    "\n",
    "\n",
    "func(1, 2)\n",
    "func(v1=123, v2=456)\n",
    "func(11, data=123)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:22:43.943488900Z",
     "start_time": "2024-01-15T02:22:43.820773500Z"
    }
   },
   "id": "f23c5a8855b966fa",
   "execution_count": 16
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.返回值\n",
    "返回值可以是任意类型"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "79578778c8ea4cc"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "def plus(v1, v2):\n",
    "    v3 = v1 + v2\n",
    "    print(v3)\n",
    "\n",
    "\n",
    "a = plus(10, 20)\n",
    "print(a)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:25:39.423076700Z",
     "start_time": "2024-01-15T02:25:39.263946100Z"
    }
   },
   "id": "518639e353f1a0a9",
   "execution_count": 19
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30\n"
     ]
    }
   ],
   "source": [
    "def plus(v1, v2):\n",
    "    v3 = v1 + v2\n",
    "    return v3\n",
    "\n",
    "\n",
    "b = plus(10, 20)\n",
    "print(b)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:25:47.951946Z",
     "start_time": "2024-01-15T02:25:47.801318500Z"
    }
   },
   "id": "8c2f2402d346b654",
   "execution_count": 20
  },
  {
   "cell_type": "markdown",
   "source": [
    "函数没有返回值，则默认返回None"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b672b9bdf13d404f"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    data = 123\n",
    "\n",
    "\n",
    "res = func()\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f2487e48defb1f56"
  },
  {
   "cell_type": "markdown",
   "source": [
    "函数内部一旦遇到return，函数立即终止"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "676eb5e5b464e38b"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    v1 = 123\n",
    "    v2 = 888\n",
    "    return 999\n",
    "    v4 = 10\n",
    "\n",
    "\n",
    "res = func()\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "29d9febd6ecdb836"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    for i in range(10):\n",
    "        return i\n",
    "\n",
    "\n",
    "res = func()\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "98a9e703e8fa42d4"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    for i in range(10):\n",
    "        break\n",
    "\n",
    "\n",
    "res = func()\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "98f07b0349e27b91"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    for i in range(10):\n",
    "        print(i)\n",
    "        print(\"end\")\n",
    "\n",
    "\n",
    "res = func()\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4387ad3ca8d912fa"
  },
  {
   "cell_type": "markdown",
   "source": [
    "返回元祖"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d855b92d5f838cc4"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    return 11, 22, 33\n",
    "\n",
    "\n",
    "v1 = func()\n",
    "print(v1)\n",
    "\n",
    "v1, v2, v3 = func()\n",
    "print(v1, v2, v3)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "58c9b8b9d41a87a7"
  },
  {
   "cell_type": "markdown",
   "source": [
    "练习"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3627a248861c4d94"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def add(*args):\n",
    "    result = 0\n",
    "    for item in args:\n",
    "        result = result + item\n",
    "    return result\n",
    "\n",
    "\n",
    "print(add(11, 22, 33))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fe4b468f5ad705e8"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def add(*args):\n",
    "    result = 0\n",
    "    for item in args:\n",
    "        if type(item) == str:\n",
    "            if item.isdecimal():\n",
    "                result = result + int(item)\n",
    "        else:\n",
    "            result = result + item\n",
    "    return result\n",
    "\n",
    "\n",
    "print(add('11', '22', '33'))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "51a8dde8a94c7a52"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    print(\"开始\")\n",
    "    for i in range(3):\n",
    "        print(i)\n",
    "    print(\"结束\")\n",
    "\n",
    "\n",
    "res = func()\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e737937c43388689"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def file_info(path, text):\n",
    "    result = []\n",
    "    f = open(path, mode='rb', encoding='utf-8')\n",
    "    for line in f:\n",
    "        line = line.strip()\n",
    "        if text not in line:\n",
    "            continue\n",
    "        result.append(line)\n",
    "\n",
    "    f.close()\n",
    "    return result"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "aa99ac1d2089b9c5"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "\n",
    "def file_info(path, text):\n",
    "    result = []\n",
    "    if not os.path.exists(path):\n",
    "        return result\n",
    "\n",
    "    with open(path, mode='rb', encoding='utf-8') as f:\n",
    "        for line in f:\n",
    "            line = line.strip()\n",
    "            if text not in line:\n",
    "                continue\n",
    "            result.append(line)\n",
    "\n",
    "    return result"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3a32bf043c4905d4"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "29a928a3cdb6c552"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.函数调用"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "de5ceeca2581bffe"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def f1():\n",
    "    print(11)\n",
    "    print(22)\n",
    "\n",
    "\n",
    "def f2():\n",
    "    print(99)\n",
    "    f1()\n",
    "    print(66)\n",
    "\n",
    "\n",
    "f2()\n",
    "v1 = f2()\n",
    "print(v1)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ccb145c7a0b0e91"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 作用域"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9af25f9273ff2299"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "name = \"wsl\"\n",
    "age = 18\n",
    "if 1 == 1:\n",
    "    size = 10\n",
    "\n",
    "for i in range(10):\n",
    "    pass\n",
    "print(name, age, size, i)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ba3331c508c372c2"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 全局变量"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3cdb33cc0f3cd625"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "NAME = \"wsl\"\n",
    "AGE = 18\n",
    "\n",
    "\n",
    "def func():\n",
    "    # 局部变量\n",
    "    v1 = 10\n",
    "    v2 = \"root\"\n",
    "\n",
    "    print(v1)\n",
    "    print(NAME)  #函数里没有，就去外层找\n",
    "\n",
    "\n",
    "func()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "49fe00a5c7be0d8c"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "USER_LIST = []\n",
    "\n",
    "\n",
    "def register():\n",
    "    while True:\n",
    "        user = input(\"请输入用户名：\")\n",
    "        if user.upper() == \"Q\":\n",
    "            return\n",
    "        USER_LIST.append(user)\n",
    "\n",
    "\n",
    "def show_info():\n",
    "    for ele in USER_LIST:\n",
    "        print(ele)\n",
    "\n",
    "\n",
    "def run():\n",
    "    register()\n",
    "\n",
    "    show_info()\n",
    "\n",
    "\n",
    "run()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6be1010f90f83ef3"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "name = \"wsl\"\n",
    "\n",
    "\n",
    "def func():\n",
    "    print(name)\n",
    "\n",
    "\n",
    "func()\n",
    "print(name)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "49c1be963ed619f0"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "name = \"wsl\"\n",
    "\n",
    "\n",
    "def func():\n",
    "    name = 'root'\n",
    "    print(name)\n",
    "\n",
    "\n",
    "func()\n",
    "print(name)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "11b1d3739d9230bd"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### global\n",
    "在局部变量中加入global表示将不再在函数里创建局部变量，而是直接操作同名的全局变量"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3eb9259e6121be7b"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "name = \"wsl\"\n",
    "\n",
    "\n",
    "def func():\n",
    "    global name\n",
    "    name = 'root'  # 将全局变量改为‘root’\n",
    "    print(name)\n",
    "\n",
    "\n",
    "func()\n",
    "print(name)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4711e69c4f448265"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "name = \"wsl\"\n",
    "\n",
    "\n",
    "def func():\n",
    "    global name\n",
    "    name.upper()  # 将全局变量改大写\n",
    "    print(name)  #改变的name没有被接收，打印的还是之前的name，str不可变原则\n",
    "\n",
    "\n",
    "func()\n",
    "print(name)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "19a562f5d5aeed17"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "name = [11, 22, 33]\n",
    "\n",
    "\n",
    "def func():\n",
    "    name = []  # 将全局变量[]\n",
    "    name.append(666)\n",
    "    print(name)\n",
    "\n",
    "\n",
    "func()\n",
    "print(name)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fdc4a5dcb99371e2"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "name = [11, 22, 33]\n",
    "\n",
    "\n",
    "def func():\n",
    "    global name\n",
    "    name = []\n",
    "    name.append(666)\n",
    "    print(name)\n",
    "\n",
    "\n",
    "func()\n",
    "print(name)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "72e0238b81bf560f"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 函数名是一个变量\n",
    "函数=变量\n",
    "函数名() 才是调用函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4200725fffb46c88"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def f1():\n",
    "    pass\n",
    "\n",
    "\n",
    "f2 = f1\n",
    "\n",
    "f1()\n",
    "f2()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a5d831a06a274b55"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def f1():\n",
    "    print(\"123\")\n",
    "\n",
    "\n",
    "f2 = f1()\n",
    "\n",
    "print(f1)\n",
    "print(f2)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7ae932b0101d77e2"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def f1():\n",
    "    print(\"123\")\n",
    "\n",
    "\n",
    "data_list = [11, 22, \"xxx\", True, f1]\n",
    "\n",
    "data_list[-1]()  #调用函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a7fffe5976eea5f3"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def send_sms():\n",
    "    print(\"发送短信提醒\")\n",
    "\n",
    "\n",
    "def send_email():\n",
    "    print(\"发送邮件提醒\")\n",
    "\n",
    "\n",
    "def send_dingding():\n",
    "    print(\"发送钉钉提醒\")\n",
    "\n",
    "\n",
    "def send_wechat():\n",
    "    print(\"发送微信提醒\")\n",
    "\n",
    "\n",
    "if 指标达标:\n",
    "    send_sms()\n",
    "    send_email()\n",
    "    send_dingding()\n",
    "    send_wechat()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e1080642b3177227"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 优化\n",
    "func_list = [send_sms, send_email, send_dingding, send_wechat]\n",
    "for func in func_list:\n",
    "    func()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e32d05ac021ce79b"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def register():\n",
    "    pass\n",
    "\n",
    "\n",
    "def login():\n",
    "    pass\n",
    "\n",
    "\n",
    "def user_info():\n",
    "    pass\n",
    "\n",
    "\n",
    "print(\"进入系统\")\n",
    "choice = input('请选择')\n",
    "if choice == '1':\n",
    "    register()\n",
    "elif choice == '2':\n",
    "    login()\n",
    "elif choice == '3':\n",
    "    user_info()\n",
    "else:\n",
    "    print(\"错误\")"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5a1e3018c38a4b49"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 优化\n",
    "mapping = {\n",
    "    \"1\": register,\n",
    "    \"2\": login,\n",
    "    \"3\": user_info,\n",
    "}\n",
    "\n",
    "func = mapping.get(choice)\n",
    "if func:\n",
    "    func()\n",
    "else:\n",
    "    print(\"错误\")"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "573e0f4f8ef2bc01"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "mapping = {\n",
    "    \"1\": (register, \"注册\"),\n",
    "    \"2\": (login, \"登录\"),\n",
    "    \"3\": (user_info, \"用户信息\"),\n",
    "    \"4\": (user_info1, \"休息休息\"),\n",
    "}\n",
    "\n",
    "line_list = []\n",
    "for k, v in mapping.items():\n",
    "    line = \"{}.{}\".format(k, v[-1])\n",
    "    line_list.append(line)\n",
    "message = \"  \".join(line_list)\n",
    "print(message)\n",
    "\n",
    "choice = input(\"选择：\")  # \"1\"\n",
    "group = mapping.get(choice)\n",
    "if group:\n",
    "    group[0]()\n",
    "else:\n",
    "    print(\"选择错误\")"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b1e8f20db777f7ea"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 匿名函数（lambda表达式）"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4f59baaf8e8d8640"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func(x, y):\n",
    "    return 100 + y"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5c14b6a315f9cd17"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "func = lambda x, y: 100 + y\n",
    "\n",
    "res = func(1, 2)\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cb0da9dad93f85c8"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "func = lambda: 100\n",
    "\n",
    "res = func()\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f1d3a836ed490972"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "func = lambda *arg, **kwargs: len(arg) + len(kwargs)\n",
    "\n",
    "res = func(11, 22, 33, a1=123)\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "573b507a79b302c2"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "v1 = [lambda x: x + 100, lambda x: x + 200, lambda x: x + 300]\n",
    "\n",
    "print(v1)\n",
    "res = v1[0](100)\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "17439105ce426031"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func(x):\n",
    "    return x + 100\n",
    "\n",
    "\n",
    "v1 = [func, func, func]"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bf712d51b3e16eed"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "56e57e138c7e81de"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 内置函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fa65a33550b94c1a"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "print(abs(-10))\n",
    "print(pow(2, 5))\n",
    "print(sum([1, 2, 3]))\n",
    "print(divmod(98, 10))\n",
    "print(round(3.14123, 2))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a9a719a585a0d74f"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "v1 = [11, 22, 33, 0]\n",
    "print(min(v1))\n",
    "print(max(v1))\n",
    "print(all(v1))  #元素是否全部都是True\n",
    "print(any(v1))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cb44c4a7eb598919"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "user = input('用户名')\n",
    "pwd = input('密码')\n",
    "is_valid = all([user, pwd])\n",
    "if not is_valid:\n",
    "    print(\"用户名和密码都不能为空，\")"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b5554dc7612016a7"
  },
  {
   "cell_type": "markdown",
   "source": [
    "sort"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "721d16673b4cd74e"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "print(sorted(v1))\n",
    "print(sorted(v1, reverse=True))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4824068459713d7"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "num_list = [\"1.Python基础.mp4\", \"2.环境搭建.mp4\", \"3.Pycharm安装.mp4\", \"10.总结\"]\n",
    "\n",
    "\n",
    "def ff(x):\n",
    "    return int(x.split('.')[0])\n",
    "\n",
    "\n",
    "v1 = sorted(num_list, key=ff)\n",
    "print(v1)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fe440157192d8521"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "v1 = sorted(num_list, key=lambda x: int(x.split('.')[0]))\n",
    "print(v1)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a0c13ce18a018e75"
  },
  {
   "cell_type": "markdown",
   "source": [
    "###  进制函数"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7e313124ed217e8"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "v1 = 90\n",
    "v2 = \"0b010101\"\n",
    "v3 = '0o102'\n",
    "v4 = '0x1ab'"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3495a0d1391b8f73"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 10 -> 2\n",
    "v1 = bin(90)\n",
    "print(v1)  # \"0b1011010\"\n",
    "\n",
    "# 2 to 10\n",
    "v2 = int(\"0b1011010\", base=2)\n",
    "print(v2)  # 90"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4a9fa6c619265c1"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "#10 -> 8\n",
    "v1 = oct(90)\n",
    "print(v1)  # \"0o132\"\n",
    "\n",
    "# 8 -> 10\n",
    "v2 = int(\"0o132\", base=8)\n",
    "print(v2)  # 90"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c662a3fe4ebfaa86"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "#10 -> 16\n",
    "v1 = hex(90)\n",
    "print(v1)  # \"0x5a\"\n",
    "\n",
    "# 16 -> 10\n",
    "v2 = int(\"0x5a\", base=16)\n",
    "print(v2)  # 90"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cd0aa89f2f8d5a7a"
  },
  {
   "cell_type": "markdown",
   "source": [
    " 练习\n",
    " 给你一个ip地址：`ip=\"192.168.11.23\"`，你将IP地址转换成二进制25252215"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4f8c3c7693e7f8d2"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "ip = \"192.168.11.23\"\n",
    "\n",
    "bin_string = \"\"\n",
    "for item in ip.split(\".\"):  # [\"192\",\"168\",\"11\",\"23\"]\n",
    "    v1 = int(item)\n",
    "    v2 = bin(v1)  # 0b1011010\n",
    "    v3 = v2[2:]  # 0b去掉\n",
    "\n",
    "    bin_string += v3\n",
    "\n",
    "result = int(bin_string, base=2)\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8de65401eb4317d"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "ip = \"192.168.11.23\"\n",
    "\n",
    "bin_list = []\n",
    "for item in ip.split(\".\"):  # [\"192\",\"168\",\"11\",\"23\"]\n",
    "    ele = bin(int(item))[2:]  # 每位10 -> 2\n",
    "    bin_list.append(ele)\n",
    "\n",
    "bin_string = \"\".join(bin_list)  # list -> str\n",
    "result = int(bin_string, base=2)  # 拼一起的结果2 -> 10\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7c24dc7d3d5d5862"
  },
  {
   "cell_type": "markdown",
   "source": [
    "#### unicode"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5d8b36813686c95a"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "print(ord('A'))\n",
    "print(chr(65))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8ccf089112f610a6"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 随机验证码"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2f3dfd89e695c5a2"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data_list = []\n",
    "for i in range(5):\n",
    "    num = random.randint(65, 90)\n",
    "    char = chr(num)\n",
    "    data_list.append(char)\n",
    "res = ''.join(data_list)\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1619229ac21e4824"
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 随机汉字"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4b22fc11f91e2afb"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data_list = []\n",
    "for i in range(20):\n",
    "    num = random.randint(20208, 20495)\n",
    "    char = chr(num)\n",
    "    data_list.append(char)\n",
    "res = ''.join(data_list)\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c4107d8f043b6a24"
  },
  {
   "cell_type": "markdown",
   "source": [
    "type"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "cc79b8a6367af81e"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func(data):\n",
    "    if type(data) == int:\n",
    "        return 111\n",
    "    elif type(data) == str:\n",
    "        return 222\n",
    "    elif type(data) == list:\n",
    "        return 333\n",
    "    else:\n",
    "        return 999\n",
    "\n",
    "\n",
    "func(11)\n",
    "func(\"xxx\")\n",
    "func([11, 22])"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1e573fdede4f8632"
  },
  {
   "cell_type": "markdown",
   "source": [
    "callable，判断是否可执行"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "862745bdefd5848"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "print(callable('abc'))\n",
    "\n",
    "\n",
    "def v1():\n",
    "    pass\n",
    "\n",
    "\n",
    "print(callable(v1))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c423ab0e6c06ee1d"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    print(999)\n",
    "\n",
    "\n",
    "data_list = [11, 22, 33, 44, 55, func]\n",
    "\n",
    "for item in data_list:\n",
    "    if callable(item):\n",
    "        item()\n",
    "    else:\n",
    "        print(item)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2604a88ec63ebc"
  },
  {
   "cell_type": "markdown",
   "source": [
    "enumerate，循环过程中，自动为你生成一列"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8666dbe3f8347737"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "goods = [\"手机\", \"电脑\", \"汽车\"]\n",
    "\n",
    "for item in goods:\n",
    "    print(item)\n",
    "\n",
    "for i in range(len(goods)):\n",
    "    print(i, goods[i])\n",
    "\n",
    "for i in range(len(goods)):\n",
    "    print(i + 1, goods[i])"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "708149b8d783b127"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "for i, item in enumerate(goods, 1):\n",
    "    print(i, item)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "aea84af9f13051c4"
  },
  {
   "cell_type": "markdown",
   "source": [
    "找到某个路径下的所有文件"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "18803af88e60ec1f"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "res = os.listdir(r\"D:\\learn\")\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "690b2454fc8fd6a4"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "3e9cf59303c37192"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 生成器函数  \n",
    "yield 类似懒加载 .__next__()才执行，\n",
    "再次遇到就从上次的位置继续执行，直到遇到StopIteration报错"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f69c893bf2d9fb23"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    print(123)\n",
    "    yield 1\n",
    "    print(456)\n",
    "    yield 2\n",
    "    print(789)\n",
    "    yield 3\n",
    "\n",
    "\n",
    "# 执行生成器函数，返回一个生成器对象\n",
    "obj = func()\n",
    "\n",
    "v1 = obj.__next__()\n",
    "print(v1)\n",
    "\n",
    "v2 = obj.__next__()\n",
    "print(v2)\n",
    "\n",
    "v3 = obj.__next__()\n",
    "print(v3)\n",
    "\n",
    "v4 = obj.__next__()\n",
    "print(v4)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4fa7b4c22fac9b12"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 优化\n",
    "def func():\n",
    "    yield 1\n",
    "    yield 2\n",
    "    yield 3\n",
    "\n",
    "\n",
    "obj = func()\n",
    "\n",
    "for item in obj:\n",
    "    print(item)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "505f061896739ef7"
  },
  {
   "cell_type": "markdown",
   "source": [
    "作用：创建极大的变量时，不需要立马在内存创建，边执行边创建"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "89ac8f565de41e2a"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def gen_big_data(limit):\n",
    "    num = 0\n",
    "    while num < limit:\n",
    "        yield num\n",
    "        num += 1\n",
    "\n",
    "\n",
    "big = gen_big_data(1000)\n",
    "\n",
    "for item in big:\n",
    "    print(item)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ef2a1e94f6ea185d"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "b97b99a03bebf5c"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 闭包\n",
    "通过函数嵌套的机制，先将数据封装到`作用域`(包)，后续再执行内部函数时，再去获取值."
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "787d4db898be2f55"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 函数嵌套\n",
    "def func():\n",
    "    print(123)\n",
    "\n",
    "    def xxx():\n",
    "        print(999)\n",
    "\n",
    "    xxx()\n",
    "\n",
    "\n",
    "func()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "80cc723054645834"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 函数嵌套 并返回\n",
    "def func():\n",
    "    print(123)\n",
    "\n",
    "    def xxx():\n",
    "        print(999)\n",
    "\n",
    "    return xxx\n",
    "\n",
    "\n",
    "v1 = func()\n",
    "v1()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f81a0a7a4638b68a"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 函数内部的对象创建后没有人用了 就销毁了\n",
    "name = \"root\"\n",
    "\n",
    "\n",
    "def func():\n",
    "    v1 = 123\n",
    "    print(v1)\n",
    "    print(name)\n",
    "\n",
    "\n",
    "func()\n",
    "print(123)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "94b89269642658bb"
  },
  {
   "cell_type": "markdown",
   "source": [
    "![jupyter](./png/img_1.png)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "6c4e189c902f05e6"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 函数内部的东西赋值给了res 不能直接销毁\n",
    "name = \"root\"\n",
    "\n",
    "\n",
    "def func():\n",
    "    v1 = 123\n",
    "\n",
    "    def inner():\n",
    "        print(666)\n",
    "        return inner\n",
    "\n",
    "\n",
    "res = func()\n",
    "print(999)\n",
    "\n",
    "res()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c57e49a0c055fa33"
  },
  {
   "cell_type": "markdown",
   "source": [
    "![jupyter](./png/img_2.png)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1155bc349498dc04"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 闭包 将n1放进函数里 用的时候再拿出来\n",
    "def func(n1):\n",
    "    v1 = 123\n",
    "\n",
    "    def inner():\n",
    "        print(n1, v1)\n",
    "\n",
    "    return inner\n",
    "\n",
    "\n",
    "res1 = func(6)\n",
    "res2 = func(7)\n",
    "res3 = func(9)\n",
    "\n",
    "res1()\n",
    "res2()\n",
    "res3()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "79d008372b77c424"
  },
  {
   "cell_type": "markdown",
   "source": [
    "结合线程池的下载 + 闭包，实现的批量下载图片："
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5081c2fae859cda1"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def task(url):\n",
    "    \"\"\" 下载 \"\"\"\n",
    "    res = requests.get(url)\n",
    "    print(res.status_code)\n",
    "\n",
    "\n",
    "pool = ThreadPoolExecutor(5)\n",
    "img_list = []\n",
    "for item in img_list:\n",
    "    fur = pool.submit(task, item['url'])"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2b1e6b77ec0ef39f"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def task(url):\n",
    "    \"\"\" 下载 \"\"\"\n",
    "    res = requests.get(url)\n",
    "    print(res.status_code)\n",
    "\n",
    "\n",
    "def done(response):\n",
    "    print(\"ok\", response.result())\n",
    "\n",
    "\n",
    "pool = ThreadPoolExecutor(5)\n",
    "img_list = []\n",
    "for item in img_list:\n",
    "    fur = pool.submit(task, item['url'])\n",
    "    fur.add_done_callback(done)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c19ed0e0a9f395e5"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import time\n",
    "import requests\n",
    "from concurrent.futures import ThreadPoolExecutor\n",
    "\n",
    "\n",
    "def task(url):\n",
    "    \"\"\" 下载 \"\"\"\n",
    "    res = requests.get(url)\n",
    "    return res.content\n",
    "\n",
    "\n",
    "def outer(file_name):\n",
    "    def done(response):\n",
    "        \"\"\" 将下载的内容写到文件，需要知道下载的那个文件名 \"\"\"\n",
    "        with open(file_name, mode='wb') as f:\n",
    "            f.write(response.result())\n",
    "\n",
    "    return done\n",
    "\n",
    "\n",
    "# 线程池中最多创建5个线程\n",
    "pool = ThreadPoolExecutor(5)\n",
    "img_list = [\n",
    "    {\"name\": \"266.png\", 'url': \"https://hbimg.huabanimg.com/51d46dc32abe7ac7f83b94c67bb88cacc46869954f478-aP4Q3V\"},\n",
    "    {\"name\": \"193.png\", 'url': \"https://hbimg.huabanimg.com/703fdb063bdc37b11033ef794f9b3a7adfa01fd21a6d1-wTFbnO\"},\n",
    "    {\"name\": \"300.png\", 'url': \"https://hbimg.huabanimg.com/2f5b657edb9497ff8c41132e18000edb082d158c2404-8rYHbw\"},\n",
    "    {\"name\": \"301.png\", 'url': \"https://hbimg.huabanimg.com/2f5b657edb9497ff8c41132e18000edb082d158c2404-8rYHbw\"},\n",
    "    {\"name\": \"302.png\", 'url': \"https://hbimg.huabanimg.com/2f5b657edb9497ff8c41132e18000edb082d158c2404-8rYHbw\"},\n",
    "    {\"name\": \"303.png\", 'url': \"https://hbimg.huabanimg.com/2f5b657edb9497ff8c41132e18000edb082d158c2404-8rYHbw\"},\n",
    "    {\"name\": \"304.png\", 'url': \"https://hbimg.huabanimg.com/2f5b657edb9497ff8c41132e18000edb082d158c2404-8rYHbw\"},\n",
    "    {\"name\": \"305.png\", 'url': \"https://hbimg.huabanimg.com/2f5b657edb9497ff8c41132e18000edb082d158c2404-8rYHbw\"},\n",
    "    {\"name\": \"306.png\", 'url': \"https://hbimg.huabanimg.com/2f5b657edb9497ff8c41132e18000edb082d158c2404-8rYHbw\"},\n",
    "    {\"name\": \"307.png\", 'url': \"https://hbimg.huabanimg.com/2f5b657edb9497ff8c41132e18000edb082d158c2404-8rYHbw\"},\n",
    "]\n",
    "for item in img_list:\n",
    "    fur = pool.submit(task, item['url'])\n",
    "    fur.add_done_callback(outer(item['name']))"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7ea882d7ea6992df"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "2fffd4671f2915d0"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 装饰器\n",
    "不改变原先方法 增强功能\n",
    "嵌套+闭包\n",
    "@XXX"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "f07b14c27ddb4627"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    print(\"我是func函数\")\n",
    "    value = \"中国联通\"\n",
    "    return value\n",
    "\n",
    "\n",
    "res = func()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ff578df0b5869281"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    print('before')\n",
    "\n",
    "    print(\"我是func函数\")\n",
    "    value = \"中国联通\"\n",
    "\n",
    "    print(\"after\")\n",
    "\n",
    "    return value"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "3e10a45ca5a67ca0"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def func():\n",
    "    print(\"我是func函数\")\n",
    "    value = \"中国联通\"\n",
    "    return value\n",
    "\n",
    "\n",
    "def xxxxx(a1):\n",
    "    # a1=原来的那个func函数\n",
    "    def inner():\n",
    "        print(\"before\")\n",
    "        result = a1()\n",
    "        print(\"after\")\n",
    "        return result\n",
    "\n",
    "    return inner\n",
    "\n",
    "\n",
    "func = xxxxx(func)  # 此行换成注解作用一样\n",
    "\n",
    "res = func()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "daeae0ab1a41c686"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 注解\n",
    "def xxxx(a1):\n",
    "    def inner():\n",
    "        print(\"before\")\n",
    "        result = a1()\n",
    "        print('after')\n",
    "        return result\n",
    "\n",
    "    return inner\n",
    "\n",
    "\n",
    "@xxxx\n",
    "def func1():\n",
    "    print(\"我是func函数\")\n",
    "    value = \"中国联通\"\n",
    "    return value\n",
    "\n",
    "\n",
    "@xxxx\n",
    "def func2():\n",
    "    print(\"我是func函数\")\n",
    "    value = \"中国联通\"\n",
    "    return value\n",
    "\n",
    "\n",
    "res1 = func1()\n",
    "res2 = func2()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5113ab754dae1885"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "def outer(func):\n",
    "    def inner():\n",
    "        # \n",
    "        res = func()\n",
    "        # \n",
    "        return res\n",
    "\n",
    "    return inner\n",
    "\n",
    "\n",
    "@outer  # show = outer(show)\n",
    "def show():\n",
    "    print(123)\n",
    "    return 999\n",
    "\n",
    "\n",
    "v1 = show()\n",
    "print(v1)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8b6a624bdd8290e8"
  },
  {
   "cell_type": "markdown",
   "source": [
    "练习"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fea65563814adaf0"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 让原函数执行5次\n",
    "def repeat(func):\n",
    "    def inner():\n",
    "        result = []\n",
    "        for i in range(5):\n",
    "            res = func()\n",
    "            result.append(res)\n",
    "        return result\n",
    "\n",
    "    return inner\n",
    "\n",
    "\n",
    "@repeat\n",
    "def f1():\n",
    "    print(\"执行函数\")\n",
    "    return 8\n",
    "\n",
    "\n",
    "v1 = f1()\n",
    "print(v1)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9fefa7a13b235560"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "\n",
    "def check_file(func):\n",
    "    def inner(file_path):\n",
    "        # 检查文件是否存在\n",
    "        if not os.path.exists(file_path):\n",
    "            return None\n",
    "        res = func(file_path)\n",
    "        return res\n",
    "\n",
    "    return inner\n",
    "\n",
    "\n",
    "@check_file\n",
    "def read_info(file_path):\n",
    "    f = open(file_path, mode='r', encoding='utf-8')\n",
    "    data = f.read()\n",
    "    f.close()\n",
    "    return data\n",
    "\n",
    "\n",
    "value = read_info(\"db.txt\")\n",
    "print(value)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "1e22bd07470e22b5"
  },
  {
   "cell_type": "markdown",
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "3738cc187b9b11b1"
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 推导式\n",
    "用更少的代码去实现某个特定的功能"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fc4f9947a757dcf2"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['用户-0', '用户-1', '用户-2', '用户-3', '用户-4', '用户-5', '用户-6', '用户-7', '用户-8', '用户-9', '用户-10', '用户-11', '用户-12', '用户-13', '用户-14', '用户-15', '用户-16', '用户-17', '用户-18', '用户-19', '用户-20', '用户-21', '用户-22', '用户-23', '用户-24', '用户-25', '用户-26', '用户-27', '用户-28', '用户-29', '用户-30', '用户-31', '用户-32', '用户-33', '用户-34', '用户-35', '用户-36', '用户-37', '用户-38', '用户-39', '用户-40', '用户-41', '用户-42', '用户-43', '用户-44', '用户-45', '用户-46', '用户-47', '用户-48', '用户-49', '用户-50']\n"
     ]
    }
   ],
   "source": [
    "data_list = []\n",
    "for i in range(51):\n",
    "    data_list.append(\"用户-{}\".format(i))\n",
    "print(data_list)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-15T02:14:26.027990Z",
     "start_time": "2024-01-15T02:14:25.937222500Z"
    }
   },
   "id": "4e63f95ab8dd728d",
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data_list = [i for i in range(51)]  # [0,1,2,3,4,5...50,]\n",
    "print(data_list)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "4d76b98597f91de6"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data_list = [100 for i in range(51)]\n",
    "print(data_list)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bfd3cab586cd8858"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data_list = [100 + i for i in range(51)]\n",
    "print(data_list)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d2b686b9bd05f81"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data_list = [(10, i) for i in range(51)]  # [ (10,0),(10,1),(10,2)... ]\n",
    "print(data_list)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "68822aa9059740f6"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data_list = [\"用户-{}\".format(i) for i in range(51)]\n",
    "print(data_list)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c88fe876b750c5ec"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data_list = [i for i in range(51) if i > 9]  # [10, ...]\n",
    "print(data_list)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d82427c709750860"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "num_list = [\"day01.Python基础.mp4\", \"day2.环境搭建.mp4\", \"day3.Pycharm安装.mp4\", \"day10.总结\", \"a1.png\"]\n",
    "\n",
    "result = [item for item in num_list if item.startswith(\"day\")]\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "eca0dbc585b831e3"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "info = {i: 123 for i in range(10)}"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "60f34ebc675086e7"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "info = {i: 123 for i in range(10) if i > 5}"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ecabdab1524b8d71"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 将列表中的每个元素维护 .mp4 剔除\n",
    "data_list = [\n",
    "    '1-5 编译器和解释器.mp4',\n",
    "    '1-17 今日作业.mp4',\n",
    "    '1-9 Python解释器种类.mp4',\n",
    "    '1-16 今日总结.mp4',\n",
    "    '1-2 课堂笔记的创建.mp4',\n",
    "    '1-15 Pycharm使用和破解（win系统）.mp4',\n",
    "    '1-12 python解释器的安装（mac系统）.mp4',\n",
    "    '1-13 python解释器的安装（win系统）.mp4',\n",
    "    '1-8 Python介绍.mp4', '1-7 编程语言的分类.mp4',\n",
    "    '1-3 常见计算机基本概念.mp4',\n",
    "    '1-14 Pycharm使用和破解（mac系统）.mp4',\n",
    "    '1-10 CPython解释器版本.mp4',\n",
    "    '1-1 今日概要.mp4',\n",
    "    '1-6 学习编程本质上的三件事.mp4',\n",
    "    '1-18 作业答案和讲解.mp4',\n",
    "    '1-4 编程语言.mp4',\n",
    "    '1-11 环境搭建说明.mp4'\n",
    "]\n",
    "\n",
    "result = [item.rsplit(\".\", maxsplit=1)[0] for item in data_list if item.endswith(\".mp4\")]\n",
    "print(result)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bada5d65a7209a2d"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "data_string = \"name=武沛齐&email=xxx@live.com&gender=男\"\n",
    "# 生成如下结构字典\n",
    "# info = {\n",
    "#     \"name\":\"武沛齐\",\n",
    "#     \"email\":\"xxx@live.com\",\n",
    "#     \"gender\":\"男\",\n",
    "# }\n",
    "\n",
    "info = {item.split(\"=\")[0]: item.split(\"=\")[1] for item in data_string.split(\"&\")}"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7b45c02539f604b7"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "info = {\n",
    "    \"name\": \"武沛齐\",\n",
    "    \"email\": \"xxx@live.com\",\n",
    "    \"gender\": \"男\",\n",
    "}\n",
    "# 生成如下结构的字符串\n",
    "# ['name=武沛齐', 'email=xxx@live.com', 'gender=男']\n",
    "# data_string = \"name=武沛齐&email=xxx@live.com&gender=男\"\n",
    "\n",
    "data_string = \"&\".join([\"{}={}\".format(k, v) for k, v in info.items()])\n",
    "print(data_string)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "ac7848aa00b4d87f"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "d86892e88dd8c2c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
